﻿using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.SalesCommand;
using T.E.ERP.Read.API.Dtos.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.SalesCommandHandler
{
    /// <summary>
    /// 退货通知单的查询命令处理方法
    /// </summary>
    public class return_notice_mainQueryCommandHandler:IRequestHandler<return_notice_mainQueryCommand,APIResult<List<return_notice_mainDtos>>>
    {

        /// <summary>
        /// 退货通知单的仓储接口
        /// </summary>
        private readonly IBaseRepository<return_notice_main> rRepo;
        /// <summary>
        /// 订单物料表的仓储接口
        /// </summary>
        private readonly IBaseRepository<OrderMaterials> oRepo;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="iRepo"></param>
        public return_notice_mainQueryCommandHandler(IBaseRepository<return_notice_main> rRepo, IBaseRepository<OrderMaterials> oRepo)
        {
            this.rRepo = rRepo;
            this.oRepo = oRepo;
        }
        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<return_notice_mainDtos>>> Handle(return_notice_mainQueryCommand request, CancellationToken cancellationToken)
        {
            var list = from a in rRepo.GetAll().Result
                       join b in oRepo.GetAll().Result on a.document_code equals b.SaleCode
                       select new return_notice_mainDtos
                       {
                           notice_id = a.notice_id,
                           document_code = a.document_code,
                           document_status = a.document_status,
                           customer_name = a.customer_name,
                           MaterialsCode = b.MaterialsCode,
                           MaterialsName = b.MaterialsName,
                           CalulateNumber = b.CalulateNumber,
                           OutBoundNumber = b.OutBoundNumber,
                           Batch = b.Batch,
                           document_date = a.document_date,
                           IsDel = a.IsDel,
                           ShenPiStatus = a.ShenPiStatus,
                       };
            if (request.document_code!=null)
            {
                list = list.Where(x => x.document_code == request.document_code);
            }
            if (!string.IsNullOrEmpty(request.customer_name))
            {
                list = list.Where(x => x.customer_name == request.customer_name);
            }
            list = list.OrderByDescending(x => x.notice_id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            APIResult<List<return_notice_mainDtos>> result = new APIResult<List<return_notice_mainDtos>>();
            result.Code = APIEnum.成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.data = list.ToList();
            result.Count = list.Count();

            return await Task.FromResult(result);
        }




    }


}
